package msg;

import java.util.*;

import user.User;

import common.interfaces.*;

/**
 * Represents a message with a location
 * 
 * @author Gal Lalouche
 */

public class Message extends AbstractHasID<Long> {
	private final long		id;
	private final Location	location;
	private final String	content;
	private final User		poster;
	private final long		msgTime;
	
	/**
	 * Creates a new message, with default time of now
	 * 
	 * @param messageId The messages's unique id
	 * @param location The messages's location
	 * @param content The message's content
	 * @param poster The message's poster
	 */
	Message(long messageId, Location location, String content, User poster) {
		this(messageId, location, content, poster, new Date().getTime());
	}
	
	/**
	 * Creates a new message
	 * 
	 * @param messageId The messages's unique id
	 * @param location The messages's location
	 * @param content The message's content
	 * @param poster The message's poster
	 * @param msgTime The message's time
	 */
	@Deprecated
	Message(long messageId, Location location, String content, User poster, Calendar msgTime) {
		this(messageId, location, content, poster, msgTime.getTimeInMillis());
	}
	
	/**
	 * Creates a new message
	 * 
	 * @param messageId The messages's unique id
	 * @param location The messages's location
	 * @param content The message's content
	 * @param poster The message's poster
	 * @param msgTime The message's time in unix time
	 */
	Message(long messageId, Location location, String content, User poster, long msgTime) {
		this.id = messageId;
		if (location == null) {
			throw new IllegalArgumentException("location is null");
		}
		this.location = location;
		if (content == null) {
			throw new IllegalArgumentException("content is null");
		}
		this.content = content;
		if (poster == null) {
			throw new IllegalArgumentException("poster is null");
		}
		this.poster = poster;
		this.msgTime = msgTime;
	}
	
	/**
	 * @return the location
	 */
	public Location getLocation() {
		return location;
	}
	
	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}
	
	/**
	 * @return the poster
	 */
	public User getPoster() {
		return poster;
	}
	
	/**
	 * @return the eventTime
	 */
	public long getTime() {
		return msgTime;
	}
	
	@Override
	public Long getId() {
		return id;
	}
	
	/**
	 * Accepts a visitor, returning the visitor's computation result
	 * 
	 * @param <T> The type of the return value
	 * @param visitor The visitor to accept
	 * @return The visitor's computation result
	 * @throws Exception
	 */
	public <T> T accept(IMessageVisitor<T> visitor) throws Exception {
		return visitor.visit(this);
	}
}
